<?php
if (!defined('IN_WONDERSHARE'))
{
	die('Hacking attempt');
}
class CategoryModule 
{
   var $db;
   var $str = '';
   function __construct()
   {
   	$this->db = $GLOBALS['db'];
   }
   function get_category_info($sql)//得到列表信息或者按照id得到信息
   { 
   	$date = array();
	$result = $this->db->query($sql);
	while($row = $this->db->fetch_array($result))
	{
	$date[] = $row;
	}
	return  $date;
   }
   
   function  update_category($sql,$type = 'add',$cat_id = 0,$parent_path = '')//修改或者增加分类
   {
   	$path = '';
   	if($type == 'add')
   	{
   	$fage = $this->db->query($sql);
   	return $fage;
   	}
   	elseif($type == 'update')
   	{
    
   	 $sql_s = 'SELECT cat_id,cat_series,parent_path FROM category WHERE  parent_id = '.$cat_id.'';
   	 
   	 $result = $this->db->query($sql_s);
 
   	 if(!empty($result))
   	 {

      while($row = $this->db->fetch_array($result))
	  {
	   
	   $cat_series = 1;
	   if(!empty($parent_path))
   	   {
   	   	$parent_path = $parent_path.','.$cat_id;
    	
   	   }else
   	   {
   	   	$parent_path = $cat_id;
   	   }
   	   $path_array = explode(',',$parent_path);
   	   $path_array = array_unique($path_array);
   	   $parent_path = implode(',',$path_array);
   	   $cat_series = count(explode(',',$parent_path))+1;
   	   $sql_u = "UPDATE category SET parent_path  = '$parent_path',cat_series = '$cat_series' 
   	   , update_user = '".$_SESSION['AName']."' ,update_time  = '".$_SERVER['REQUEST_TIME']."' 
   	   , modify_status = 1 WHERE  cat_id = '".$row['cat_id']."' ";
   	   $this->db->query($sql_u);
	   $this->update_category('','update',$row['cat_id'],$parent_path);
	  }
   	 }
   	}
   }
   	
   function  change_sort($sql)//改变排序
   {
   	$this->db->query($sql);
   }
   function  del_category($cat_id,$str_c="")//删除分类以及下面的子分类
   {
  
    $this->str = empty($str_c)?$cat_id:$this->str.','.$cat_id;
    
   	$sql_d = "UPDATE  category  SET is_delete = 1 , update_user = '".$_SESSION['AName']."' ,
   	update_time  = '".$_SERVER['REQUEST_TIME']."'  WHERE cat_id = '".$cat_id."'";
   	
   	$this->db->query($sql_d);
   	$sql_s = 'SELECT cat_id FROM category WHERE  parent_id = '.$cat_id.'';
   	$result = $this->db->query($sql_s);
    while($row = $this->db->fetch_array($result))
	{
	  $this->del_category($row['cat_id'],$this->str);
	}

	return  $this->str;
   }
}
?>